package com.awesome.common.base.mvp

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.awesome.common.util.ui.StatusBarUtil
import java.lang.reflect.ParameterizedType

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date:2021/4/8 11:32
 * Description:The base of activity.If want to use MVP frame,please inherit this.
 */
abstract class AbsMVPActivity<V : IBaseView, P : IPresenter<V>> : AppCompatActivity(), IBaseView {
    protected var presenter: P? = null
//    private SlideBackLayout mSlideBackLayout;

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Provided layoutId to setContentView()
     * @return The layoutId of this activity.
     */
    abstract fun initLayoutId(): Int

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do UI init operation,such as findViewByID,setAdapter and so on.
     */
    abstract fun initView()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do setListener/setCallback operation.
     */
    abstract fun setListener()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2019/4/8 11:34
     * Description:Do data init operation,such as network request,read database,read file and so on.
     */
    abstract fun initData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (immersive()) {
            // 使内容延伸到状态栏下
            StatusBarUtil.setStatusBarTranslucent(window, true)
            // 使状态栏透明
            StatusBarUtil.setStatusBarColor(window, Color.TRANSPARENT, false)
        } else {
            StatusBarUtil.setStatusBarTranslucent(window, false)
            StatusBarUtil.setStatusBarColor(window, initStatusBarColor(), false)
        }
        // 状态栏深色图标
        StatusBarUtil.setStatusBarStyle(window, initStatusBarDark())
        setContentView(initLayoutView())
//        mSlideBackLayout = new SlideBackLayout(getContext());
//        mSlideBackLayout.bindActivity(getActivity());
        presenter = createPresenter()?.also { it.attachView(this as V) }
        initView()
        setListener()
        initData()
    }

    private fun createPresenter(): P? {
        try {
            val clazz = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<P>
            return clazz.newInstance()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    open fun initLayoutView(): View {
        return LayoutInflater.from(getContext()).inflate(initLayoutId(), null)
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:39
     * Description:Make the activity immersive or not,if enable immersion,the status bar will transparent.
     * @return Returns true to enable immersion,false not to enable immersion.
     */
    open fun immersive(): Boolean = false

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:44
     * Description:Set the color of status bar.
     * @return Color's value.
     */
    open fun initStatusBarColor(): Int = 0xFF000000.toInt()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:45
     * Description:Set the mode of icon in status bar
     * @return Returns true to start dark mode,false to start light mode.
     */
    open fun initStatusBarDark(): Boolean = false

    fun getContext(): Context = this

    fun getActivity(): FragmentActivity = this

    open fun toastShort(content: String) = Toast.makeText(this, content, Toast.LENGTH_SHORT).show()

    open fun toastLong(content: String) = Toast.makeText(this, content, Toast.LENGTH_LONG).show()

//    /**
//     * Author: JFangZ
//     * Email: janezhang1901@163.com
//     * Date:2019/6/10 16:26
//     * Description:取消侧滑关闭 Activity 功能
//     */
//    public void unbindSlideBackActivity() {
//        mSlideBackLayout.unbindActivity();
//    }
}
